﻿using System;
using System.Data;
using eTMS.Controllers.SoNoSinhVienService;
using System.Collections.Generic;
using eTMS.Controllers.KhoaHocService;
namespace eTMS.Controllers
{
    public class SoNoSinhVienController : SoNoSinhVienControllerBase
    {
        public SoNoSinhVienController()
            : base()
        {
        }
        private DataTable _tblChiTiet = new DataTable();
        public DataTable tblChiTiet
        {
            get
            {
                return _tblChiTiet;
            }
        }
        public void LoadSoNoByNamHocHocKy(int SinhVien_ID, int NamHoc_ID, int HocKy_ID)
        {
            List<SoNoSinhVienEntity> ls = new List<SoNoSinhVienEntity>(dao.SelectAllFilter("NamHoc_ID=" + NamHoc_ID.ToString() + " AND HocKy_ID=" + HocKy_ID.ToString() + " AND SinhVien_ID=" + SinhVien_ID.ToString()));
            if (ls.Count > 0)
                SoNoSinhVienObject = ls[0];
            else
                SoNoSinhVienObject = new SoNoSinhVienEntity();
        }
        public void LoadAllDSSoNoChiTiet()
        {
            SoNoSinhVienChiTietService.SoNoSinhVienChiTietServiceClient w = new eTMS.Controllers.SoNoSinhVienChiTietService.SoNoSinhVienChiTietServiceClient();
            _tblChiTiet = w.SelectAllDSWithKhoanTienBySoNoSinhVien_ID(SoNoSinhVienObject.SoNoSinhVien_ID).Tables[0];
            AddIndexToTable(_tblChiTiet);
        }
        public void SaveKhoanTienSoNoChiTietToListSinhVien(int NamHoc_ID, int HocKy_ID, int KhoanTien_ID, decimal SoTien, List<DataRow> lsSinhVienRow)
        {
            SoNoSinhVienChiTietService.SoNoSinhVienChiTietServiceClient wChiTiet = new eTMS.Controllers.SoNoSinhVienChiTietService.SoNoSinhVienChiTietServiceClient();
            List<SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity> lsSoNoChiTiet = new List<SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity>();

            List<SoNoSinhVienService.SoNoSinhVienEntity> lsSoNo = new List<SoNoSinhVienEntity>();
            List<string> lsSinhVien_ID = new List<string>();
            foreach (DataRow r in lsSinhVienRow)
            {
                lsSinhVien_ID.Add(r["SinhVien_ID"].ToString());
            }
            if (lsSinhVien_ID.Count > 0)
                lsSoNo = new List<SoNoSinhVienEntity>(dao.SelectAllFilter("HocKy_ID=" + HocKy_ID.ToString() + " AND NamHoc_ID=" + NamHoc_ID.ToString() + " AND SinhVien_ID IN(" + string.Join(",", lsSinhVien_ID.ToArray()) + ")"));
            List<string> lsSoNo_ID = new List<string>();
            foreach (SoNoSinhVienEntity oSoNo in lsSoNo)
            {
                lsSoNo_ID.Add(oSoNo.SoNoSinhVien_ID.ToString());
            }
            if (lsSoNo_ID.Count > 0)
                lsSoNoChiTiet = new List<SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity>(wChiTiet.SelectAllFilter("SoNoSinhVien_ID IN(" + string.Join(",", lsSoNo_ID.ToArray()) + ") AND KhoanTien_ID=" + KhoanTien_ID.ToString()));
            foreach (DataRow r in lsSinhVienRow)
            {
                int SinhVien_ID = ConvertInt(r["SinhVien_ID"]);
                SoNoSinhVienEntity oSoNo = lsSoNo.Find(delegate(SoNoSinhVienEntity oFind) { return oFind.SinhVien_ID.Equals(SinhVien_ID); });
                SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity oChiTiet = new eTMS.Controllers.SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity();
                if (oSoNo == null)
                {
                    oSoNo = new SoNoSinhVienEntity();
                    oSoNo.SinhVien_ID = SinhVien_ID;
                    oSoNo.NamHoc_ID = NamHoc_ID;
                    oSoNo.HocKy_ID = HocKy_ID;
                    oSoNo.NgayCN = DateTime.Now;
                    oSoNo.GhiChu = "";
                    oSoNo = dao.SaveRet(oSoNo);
                }
                else
                {
                    oChiTiet = lsSoNoChiTiet.Find(delegate(SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity oFind) { return oFind.SoNoSinhVien_ID.Equals(oSoNo.SoNoSinhVien_ID); });
                }
                oChiTiet.SoNoSinhVien_ID = oSoNo.SoNoSinhVien_ID;
                oChiTiet.KhoanTien_ID = KhoanTien_ID;
                oChiTiet.SoTien = SoTien;
                if (oChiTiet.SoNoSinhVienChiTiet_ID > 0)
                {
                    wChiTiet.Update(oChiTiet);
                }
                else
                {
                    wChiTiet.Save(oChiTiet);
                }
            }
        }
        private void SaveKhoanTienSoNoChiTietToListSinhVien(int NamHoc_ID, int HocKy_ID, int KhoanTien_ID, List<decimal> lsSoTien, List<DataRow> lsSinhVienRow)
        {
            SoNoSinhVienChiTietService.SoNoSinhVienChiTietServiceClient wChiTiet = new eTMS.Controllers.SoNoSinhVienChiTietService.SoNoSinhVienChiTietServiceClient();
            List<SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity> lsSoNoChiTiet = new List<SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity>();
            List<SoNoSinhVienService.SoNoSinhVienEntity> lsSoNo = new List<SoNoSinhVienEntity>();
            List<string> lsSinhVien_ID = new List<string>();
            foreach (DataRow r in lsSinhVienRow)
            {
                lsSinhVien_ID.Add(r["SinhVien_ID"].ToString());
            }
            if (lsSinhVien_ID.Count > 0)
                lsSoNo = new List<SoNoSinhVienEntity>(dao.SelectAllFilter("HocKy_ID=" + HocKy_ID.ToString() + " AND NamHoc_ID=" + NamHoc_ID.ToString() + " AND SinhVien_ID IN(" + string.Join(",", lsSinhVien_ID.ToArray()) + ")"));
            List<string> lsSoNo_ID = new List<string>();
            foreach (SoNoSinhVienEntity oSoNo in lsSoNo)
            {
                lsSoNo_ID.Add(oSoNo.SoNoSinhVien_ID.ToString());
            }
            if (lsSoNo_ID.Count > 0)
                lsSoNoChiTiet = new List<SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity>(wChiTiet.SelectAllFilter("SoNoSinhVien_ID IN(" + string.Join(",", lsSoNo_ID.ToArray()) + ") AND KhoanTien_ID=" + KhoanTien_ID.ToString()));
            for (int i = 0; i < lsSinhVienRow.Count; i++)
            {
                DataRow r = lsSinhVienRow[i];
                int SinhVien_ID = ConvertInt(r["SinhVien_ID"]);
                SoNoSinhVienEntity oSoNo = lsSoNo.Find(delegate(SoNoSinhVienEntity oFind) { return oFind.SinhVien_ID.Equals(SinhVien_ID); });
                SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity oChiTiet = new eTMS.Controllers.SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity();
                if (oSoNo == null)
                {
                    oSoNo = new SoNoSinhVienEntity();
                    oSoNo.SinhVien_ID = SinhVien_ID;
                    oSoNo.NamHoc_ID = NamHoc_ID;
                    oSoNo.HocKy_ID = HocKy_ID;
                    oSoNo.NgayCN = DateTime.Now;
                    oSoNo.GhiChu = "";
                    oSoNo = dao.SaveRet(oSoNo);
                }
                else
                {
                    oChiTiet = lsSoNoChiTiet.Find(delegate(SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity oFind) { return oFind.SoNoSinhVien_ID.Equals(oSoNo.SoNoSinhVien_ID); });
                }
                oChiTiet.SoNoSinhVien_ID = oSoNo.SoNoSinhVien_ID;
                oChiTiet.KhoanTien_ID = KhoanTien_ID;
                oChiTiet.SoTien = lsSoTien[i];
                if (oChiTiet.SoNoSinhVienChiTiet_ID > 0)
                {
                    wChiTiet.Update(oChiTiet);
                }
                else
                {
                    wChiTiet.Save(oChiTiet);
                }
            }
        }
        public void TongHopHocPhi(int NamHoc_ID, int HocKy_ID, int KhoaHoc_ID, int KhoanTien_ID)
        {
            NamHocService.NamHocEntity oNamHoc = NamHocController.FindNamHoc(NamHoc_ID);
            ChuongTrinhDaoTaoController ctrl = new ChuongTrinhDaoTaoController();
            DataTable tblSinhVien;
            SinhVienService.SinhVienServiceClient wSinhVien = new SinhVienService.SinhVienServiceClient();
            DangKyHocPhanService.DangKyHocPhanServiceClient wDangKyHP = new eTMS.Controllers.DangKyHocPhanService.DangKyHocPhanServiceClient();
            decimal HocPhiUnit = (decimal)GetDoubleConfig("PAY_PER_UNIT");
            List<KhoaHocEntity> lsKhoaHoc = new List<KhoaHocEntity>();
            if (KhoaHoc_ID > 0)
            {
                lsKhoaHoc.Add(KhoaHocController.FindKhoaHoc(KhoaHoc_ID));
            }
            else
            {
                foreach (KhoaHocEntity oKhoaHoc in KhoaHocController.ListKhoaHocStatic)
                {
                    lsKhoaHoc.Add(oKhoaHoc);
                }
            }
            int currentCTDT = 0;
            foreach (NganhHocService.NganhHocEntity oNganhHoc in NganhHocController.ListNganhHocStatic)
            {
                foreach (KhoaHocEntity oKhoaHoc in lsKhoaHoc)
                {
                    int NamThu = GetNamThu(oKhoaHoc.BatDau, oNamHoc.NgayBatDau);
                    ctrl.LoadChuongTrinhDaoTaoSuDungByKhoaNganh(oKhoaHoc.KhoaHoc_ID, oNganhHoc.NganhHoc_ID);
                    tblSinhVien = wSinhVien.SearchSinhVien_ID(-1, oNganhHoc.NganhHoc_ID, -1, oKhoaHoc.KhoaHoc_ID, null).Tables[0];
                    if (ctrl.ChuongTrinhDaoTaoObject.LoaiHocPhi == 0)
                    {
                        if (ctrl.ChuongTrinhDaoTaoObject.ChuongTrinhDaoTao_ID != currentCTDT)
                        {
                            ctrl.LoadAllHocPhiTheoKy();
                        }
                        foreach (DataRow r in ctrl.TableHocPhiTheoKy.Select("NamThu=" + NamThu.ToString() + " AND HocKy_ID=" + HocKy_ID.ToString()))
                        {
                            SaveKhoanTienSoNoChiTietToListSinhVien(NamHoc_ID, HocKy_ID, ConvertInt(r["KhoanTien_ID"]), ConvertNumber(r["SoTien"]), new List<DataRow>(tblSinhVien.Select()));
                        }
                    }
                    else if (ctrl.ChuongTrinhDaoTaoObject.LoaiHocPhi == 1)
                    {
                        List<string> lsSinhVien_ID = new List<string>();
                        foreach (DataRow r in tblSinhVien.Rows)
                        {
                            lsSinhVien_ID.Add(r["SinhVien_ID"].ToString());
                        }
                        string filter = "SinhVien_ID IN(" + string.Join(",", lsSinhVien_ID.ToArray()) + ") AND HocKy_ID=" + HocKy_ID.ToString() + " AND NamHoc_ID=" + NamHoc_ID.ToString();
                        List<decimal> lsSoTien = new List<decimal>();
                        DataTable tblDangKy = wDangKyHP.SelectAllDSFilter(filter).Tables[0];
                        foreach (DataRow r in tblSinhVien.Rows)
                        {
                            lsSoTien.Add(0);
                            DataRow[] rs = tblDangKy.Select("SinhVien_ID=" + r["SinhVien_ID"].ToString());
                            foreach (DataRow rDangKy in rs)
                            {
                                MonHocService.MonHocEntity oMonHoc = MonHocController.FindMonHoc(ConvertInt(rDangKy["MonHoc_ID"]));
                                lsSoTien[lsSoTien.Count - 1] += HocPhiUnit * oMonHoc.SoHocPhanHocPhi;
                            }
                        }
                        SaveKhoanTienSoNoChiTietToListSinhVien(NamHoc_ID, HocKy_ID, KhoanTien_ID, lsSoTien, new List<DataRow>(tblSinhVien.Select()));
                    }
                    currentCTDT = ctrl.ChuongTrinhDaoTaoObject.ChuongTrinhDaoTao_ID;
                }
            }
        }
    }
}
